<?php
	if($_POST["mag_year"]!=0){
		require_once("../connection.php");
		$connection=mysql_connect($server,$username,$password);
		if($connection){
			$database=mysql_select_db($db, $connection);
			$sql="SELECT mag_folio
				FROM magove_t_folios
				WHERE mag_year=".$_POST["mag_year"]."
				ORDER BY mag_folio DESC
				LIMIT 1;";
			$query=mysql_query($sql);
			$row=mysql_fetch_array($query);
			echo str_pad($row["mag_folio"]+1,4,"0",STR_PAD_LEFT);
			$connection=mysql_close();
		}
	}
	else{
		echo "";
	}

	if($_POST["mag_folio"]!=0){
		require_once("../connection.php");
		$connection=mysql_connect($server,$username,$password);
		if($connection){
			$database=mysql_select_db($db, $connection);
			$sql="SELECT mag_folio
				FROM magove_t_folios
				WHERE mag_year=".$_POST["mag_year"]."
				ORDER BY mag_folio DESC
				LIMIT 1;";
			$query=mysql_query($sql);
			$row=mysql_fetch_array($query);
			echo str_pad($row["mag_folio"]+1,4,"0",STR_PAD_LEFT);
			$connection=mysql_close();
		}
	}
	else{
		echo "";
	}
	if($_POST["mag_id"]!=0){
		require_once("../connection.php");
		$connection=mysql_connect($server,$username,$password);
		if($connection){
			$database=mysql_select_db($db, $connection);
			$sql="SELECT mag_folio, mag_comment
				FROM magove_t_folios
				WHERE mag_id=".$_POST["mag_id"]."
				ORDER BY mag_folio DESC
				LIMIT 1;";
			$query=mysql_query($sql);
			$row=mysql_fetch_array($query);
			$return_array='{"mag_folio" : "'.str_pad($row["mag_folio"]+1,4,"0",STR_PAD_LEFT).'", "mag_comment" : "'.str_replace("\"", "'",str_replace("\r\n", "linebreak",$row["mag_comment"])).'"}';
			$connection=mysql_close();
			echo $return_array;
		}
	}
	else{
		echo "";
	}

?>
